package com.example.front.controller;

import com.example.common.dto.JsonResult;
import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.front.dto.*;
import com.example.front.entity.Craft;
import com.example.front.entity.Line;
import com.example.front.serivce.CraftService;
import com.example.front.serivce.LineService;
import com.example.interceptor.UserContext;
import com.example.passport.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "工艺管理")
@RestController
@CrossOrigin
@RequestMapping("/front/craft")
public class CraftController {

    @Resource
    CraftService craftService;

    @ApiOperation("添加工艺")
    @PostMapping("/create")
    public JsonResult<String> create(@Valid CraftDto craftDto) {
        User user = UserContext.getUser();
        Craft craft = new Craft();
        BeanUtils.copyProperties(craftDto, craft);
        craft.setTenantId(user.getTenantId());
        craftService.createCraft(craft);
        return new JsonResult<>(true, "添加成功");
    }

    @ApiOperation("删除工艺")
    @PostMapping("/delete")
    public JsonResult<String> delete(Long id){
        craftService.deleteCraft(id);
        return new JsonResult<>(true, "删除成功");
    }

    @ApiOperation("修改数据")
    @PostMapping("/update")
//    public JsonResult<String> update(@Valid CraftDto craftDto){
//        Craft craft = new Craft();
//        BeanUtils.copyProperties(craftDto, craft);
//        craftService.updateCraft(craft);
//        return new JsonResult<>(true, "修改成功");
//    }
//
//    @ApiOperation("修改数据")
//    @PostMapping("/updateByCraftId")
    public JsonResult<String> updateByCraftId(@Valid Craft craftDto){
        craftService.updateCraft(craftDto);
        return new JsonResult<>(true, "修改成功");
    }

    @ApiOperation("工艺列表")
    @GetMapping("/list")
    public JsonResult<ListDto<CraftDto>> list(CraftSearchDto craftDto, Pagination pagination){
        Craft craft = new Craft();
        BeanUtils.copyProperties(craftDto, craft);
        return new JsonResult<>(craftService.listCraft(craft,pagination));
    }

}
